import { stringify } from 'qs';
import request, { fingerprintFetch } from '@/utils/request';
import { withProcessedData } from '@/utils/marketingMap/utils';
import { downloadArrayBufferFile, getMd5, getToken } from '@/utils';
import { message } from 'antd';

const basePath = '/msmarketingmap/aftersales/overall';

// 1.热力图
export const fetchHeatmap = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/residency/zoomOut?${stringify(query)}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });
// 2.格子图
export const fetchGridmap = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/residency/zoomIn?${stringify(query)}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });

// 3.查询店铺分布及概况数据
export const fetchOverview = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/store/overview?${stringify(query)}`);

    if (result.statusCode !== '0') return {};

    return result.data;
  });

export const fetchMapAll = (query: QueryProps) => {
  return Promise.all([fetchHeatmap(query), fetchGridmap(query), fetchOverview(query)]);
};

// 用户画像
// 4.常驻地排名
export const fetchResident = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/persona/residency/rank?${stringify(query)}`);

    if (result.statusCode !== '0') return {};

    return result.data;
  });

// 5.用户画像-车系分布
export const fetchPersonaSeries = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/persona/series?${stringify(query)}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });

// 6.用户画像-项目分布 ** 接口文档描述错误字段暂未修改
export const fetchPersonaProject = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/persona/project?${stringify(query)}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });
// 7.用户画像-用户类型分布 ** 二阶段需求，12.25版本目前还没有使用 **
export const fetchPersonaUserType = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/persona/user/type?${stringify(query)}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });

export const fetchPersonaPortraitInfo = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/persona/portrait/info?${stringify({...query, tags: 4})}`);

    if (result.statusCode !== '0') return [];

    return result.data;
  });

export const fetchUserProfile = (query: QueryProps) => {
  return Promise.all([
    fetchResident(query),
    fetchPersonaSeries(query),
    fetchPersonaProject(query),
    fetchPersonaUserType(query),
    fetchPersonaPortraitInfo(query),
  ]);
};

// 与友商对比
// 8.与友商对比-售后流量对比
export const fetchTraffic = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/comparison/traffic?${stringify(query)}`);

    if (result.statusCode !== '0') return {};

    return result.data;
  });

// 9.友商对比-售后项目
export const fetchComparisonProject = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/comparison/project?${stringify(query)}`);

    if (result.statusCode !== '0') return {};

    return result.data;
  });

// 9.友商对比-售后车系
export const fetchComparisonSeries = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const result = await request(`${basePath}/comparison/series?${stringify(query)}`);

    if (result.statusCode !== '0') return {};

    return result.data;
  });

export const fetchComparison = (query: QueryProps) => {
  return Promise.all([fetchComparisonProject(query), fetchComparisonSeries(query)]);
};

// 导出
export const exportVehicle = async (query: QueryProps) =>
  withProcessedData(query, async query => {
    const params = stringify(query);
    const url = `${window.g.url}${basePath}/export?${params}`;

    fingerprintFetch(url, {
      method: 'POST',
      // body: json,
      headers: {
        'Content-Type': 'application/json;charset=UTF-8',
        OptAuthorization: `${getToken()}`,
        shaSign: getMd5(params),
      },
    })
      .then(async response => response.blob()) // 转换响应为blob
      .then((blob: any) => {
        if (!blob.size) {
          message.error('当前条件下，暂无数据');
          return;
        }

        downloadArrayBufferFile(blob, '车辆信息.xlsx');
      });
  });
